from socket import socket
from json import loads,dumps
import base64
import re
import os

def main():
    # 0.定义你要传输的文件
    file = r'D:\迅雷下载\securecrt_39196.zip'
    # 1.创建套接字对象默认使用IPv4和TCP协议
    client = socket()
    # 2.连接到服务器(需要指定IP地址和端口)
    client.connect(('192.168.0.6', 6789))
    # 3.发送内容给服务端
    client.sendall(dumps(file).encode('utf-8'))
    # 4.从服务器接收数据
    full_data = bytes()
    data = client.recv(1024)
    while data:
        full_data += data
        data = client.recv(1024)
    file = loads(full_data.decode('utf-8'))
    fileName = os.path.basename(file['fileName'])
    print(fileName)
    fileData = base64.b64decode(file['fileData'])
    with open("save_" + fileName, 'wb') as f:
        f.write(fileData)
    client.close()

if __name__ == '__main__':
    main()